/*
 *  WordData.mm
 *  MemWorld
 *
 *  Created by 付卓 on 5/29/11.
 *  Copyright 2011 com.fuzhuo.soft. All rights reserved.
 *
 */

#include "WordData.h"

// member functions for class Word
void Word::setValue(int _index,QString _name,int _value){
	index=_index;name=_name;value = _value;
}

//======= member functinos for class WordData ============

WordData::WordData(QString _dbName)
{
	cout<<"WordData 构造函数"<<endl;
	//尝试打开数据库
	dbName = _dbName;
	dataName = _dbName.append(".dat");
	printf("Word Data dbName[%s],dataName[%s]\n", dbName.latin1(), dataName.latin1());
    cetdb = new PYDB(dbName);
	initialWord();//初始化各变量
}
WordData::~WordData()
{
	cout<<"destructor of WordData!"<<endl;
	saveWords();
	//关闭数据库
	//释放数据结构
	//delete[] words;
	delete[] tmpwords;
}

string WordData::printIndex()
{
	stringstream tmp;
	tmp<<"index: ";
	for(int i=0;i<tmpmax;i++)
	{
		tmp<<tmpwords[i].index;
		tmp<<" ";
	}
	
	/* just for test */
	saveWords();
	return tmp.str();
}
void WordData::changeWord(bool right)
{
	if(memword == tmptotal-1) return;//这说明记完了
	switch(tmpwords[0].value)
	{
		case -1://如果处于初始状态，对，则认为不需要记了；错，则将其要记的次数设为5，放到1号去
			if(!right) {tmpwords[0].value=5;Word tmp=tmpwords[0];tmpwords[0]=tmpwords[STEP1];tmpwords[STEP1]=tmp;}
			else
			{
				tmpwords[0].value=MEM;memword++;
				for(int i=0;i<tmpmax;i++)
				{
					if(i<tmpmax-1) tmpwords[i]=tmpwords[i+1];
					else
					{
						if(theNextIndex!=-1) tmpwords[i]=getNext(theNextIndex);//words[theNextIndex];
					}
				}
				if(theNextIndex<tmptotal-1 && theNextIndex!=-1) theNextIndex++;
				else {tmpmax--;theNextIndex=-1;}
			}
			break;
		case 5://如果需要记5次，如果错，放到第1个，计数次数不变；如果对，放到第3个，记忆值减1
			if(!right) {Word tmp=tmpwords[0];tmpwords[0]=tmpwords[STEP1];tmpwords[STEP1]=tmp;}
			else
			{
				tmpwords[0].value=4;
				Word tmp=tmpwords[0];
				for(int i=0;i<STEP2 && i<tmpmax;i++)
				{
					tmpwords[i]=tmpwords[i+1];
				}
				if(STEP2<tmpmax-1) tmpwords[STEP2]=tmp;
				else tmpwords[tmpmax-1]=tmp;
			}
			break;
		case 4://如果需要记4次，如果错，放到第1个，记忆值加1；如果对，放到第7个，记忆值减1
			if(!right) {tmpwords[0].value=5;Word tmp=tmpwords[0];tmpwords[0]=tmpwords[STEP1];tmpwords[STEP1]=tmp;}
			else
			{
				tmpwords[0].value=3;
				Word tmp=tmpwords[0];
				for(int i=0;i<STEP3 && i<tmpmax;i++)
				{
					tmpwords[i]=tmpwords[i+1];
				}
				if(STEP3<tmpmax-1) tmpwords[STEP3]=tmp;
				else tmpwords[tmpmax-1]=tmp;
			}
			break;
		case 3://
			if(!right) {tmpwords[0].value=4;Word tmp=tmpwords[0];tmpwords[0]=tmpwords[STEP1];tmpwords[STEP1]=tmp;}
			else
			{
				tmpwords[0].value=2;
				Word tmp=tmpwords[0];
				for(int i=0;i<STEP4 && i<tmpmax;i++)
				{
					tmpwords[i]=tmpwords[i+1];
				}
				if(STEP4<tmpmax-1) tmpwords[STEP4]=tmp;
				else tmpwords[tmpmax-1]=tmp;
			}
			break;
		case 2://
			if(!right) {tmpwords[0].value=3;Word tmp=tmpwords[0];tmpwords[0]=tmpwords[STEP1];tmpwords[STEP1]=tmp;}
			else
			{
				tmpwords[0].value=1;
				Word tmp=tmpwords[0];
				for(int i=0;i<STEP5 && i<tmpmax;i++)
				{
					tmpwords[i]=tmpwords[i+1];
				}
				if(STEP5<tmpmax-1) tmpwords[STEP5]=tmp;
				else tmpwords[tmpmax-1]=tmp;
			}
			break;
		case 1://
			if(!right) {tmpwords[0].value=2;Word tmp=tmpwords[0];tmpwords[0]=tmpwords[STEP1];tmpwords[STEP1]=tmp;}
			else
			{
				tmpwords[0].value=MEM;memword++;
				for(int i=0;i<tmpmax;i++)
				{
					if(i<tmpmax-1) tmpwords[i]=tmpwords[i+1];
					else
					{
						if(theNextIndex!=-1) tmpwords[i]=getNext(theNextIndex);
					}
				}
				if(theNextIndex<tmptotal-1 && theNextIndex!=-1) theNextIndex++;
				else {tmpmax--;theNextIndex=-1;}
			}
			break;
		default:break;
	}
}
void WordData::initialWord()
{
	printf("initialWord\n");
	
	bool loadOK = loadWords();
	cetdb->find(QString("configs"), QString("key"), QString("total"));
	tmptotal = cetdb->getresult().toInt(); 
	if(!loadOK)
	{
		memword=-1;
		//tmptotal=TOTAL;//TOTAL为总单词数
		//words = new Word[TOTAL];
		if(MAX<=tmptotal) {theNextIndex=MAX;tmpmax=MAX;}
		else {tmpmax=tmptotal;theNextIndex=-1;}
        tmpwords = new Word[tmpmax];//初始化内存中单词列表
		//如果不是第一次打开，在这里会重新得到memword、tmpmax、tmptotal、theNextIndex的值
	}

	printf("tmptotal = %d, tmpmax = %d, theNextIndex=%d\n", tmptotal, tmpmax, theNextIndex);

	if(loadOK)
	{
		printf("load all words\n");
		for(int i=0;i<tmpmax;i++){
			printf("index[%d] --> eng['%s'][%d]\n", tmpwords[i].index, tmpwords[i].name.latin1(), tmpwords[i].value);
		}
	}
	else{
		printf("get all words\n");
		for(int i=0;i<tmpmax;i++){
			tmpwords[i]=getNext(i);//获得第i个单词,将前tmpmax个单词写入tmpwords数组里
			printf("index[%d] --> eng['%s'][%d]\n", tmpwords[i].index, tmpwords[i].name.latin1(), tmpwords[i].value);
		}
	}
}
//========================================Member functions=============
void WordData::saveWords()
{
    printf("saveWords start---------------------------\n");
	QFile f(dataName);
	if(f.exists())
	{
		printf("delete exist file save.dat");
		f.remove();
	}

	//写入memword tmpmax tmptotal theNextIndex
	if( !f.open( IO_ReadWrite))
	{
		printf("file open failed\n");
		return;
	}
	QTextStream stream(&f);
	stream<<memword<<"\n";
	stream<<tmpmax<<"\n";
	stream<<tmptotal<<"\n";
	stream<<theNextIndex<<"\n";
	//写入tmpwords(index, value)
	for(int i=0; i<tmpmax; i++)
	{
		stream<<tmpwords[i].index<<"\n";
		stream<<tmpwords[i].value<<"\n";
	}
    printf("saveWords end---------------------------\n");
	f.close();
}
bool WordData::loadWords()
{
	QFile f(dataName);
	if(!f.exists())
	{
		printf("loadWords failed save.dat not exist\n");
		return false;
	}
	if(!f.open(IO_ReadWrite))
	{
		printf("open file error\n");
		return false;
	}
	QTextStream stream(&f);
	QString line;
	//读入 memword tmpmax tmptotal theNextIndex
	line = stream.readLine();
	memword = line.toInt();
	line = stream.readLine();
	tmpmax = line.toInt();
	line = stream.readLine();
	//tmptotal = line.toInt();	//跳过tmptotal的值,这个值，从数据库中获得
	line = stream.readLine();
	theNextIndex = line.toInt();

	printf("load Words and ------>\ntmptotal = %d, tmpmax = %d, theNextIndex=%d\n", tmptotal, tmpmax, theNextIndex);
	tmpwords = new Word[tmpmax];//初始化tmpwords数组

	for(int i=0; i<tmpmax; i++)
	{
		line = stream.readLine();
		tmpwords[i].index=line.toInt();
		line = stream.readLine();
		tmpwords[i].value=line.toInt();
		cetdb->find(QString("engs"),QString("num"),QString("%1").arg(tmpwords[i].index));
		tmpwords[i].name = cetdb->getresult();
	}
	return true;
}
Word WordData::getNext(int i)
{
	Word tmp;//=words[i];
	// 通过数据库id字段查找出需要的词语及意义
	QString table = QString("engs");
	QString tittle = QString("num");
	QString src=QString("%1").arg(i);
	QString ans;
	cetdb->find(table, tittle, src);
	ans = cetdb->getresult();
	tmp.setValue(i, ans, ORI);
	return tmp;
}
